from PyQt5 import uic
from PyQt5.QtWidgets import QApplication
import math


class Stats:

    def __init__(self):
        # 从文件中加载UI定义
        self.ui = uic.loadUi("tool.ui")

        # 单板连接
        self.ui.pushButton.clicked.connect(self.singleHandle)
        self.ui.pushButton.setToolTip("点击计算结果")
        self.ui.axialPressure.setReadOnly(True)
        self.ui.bendCapacity.setReadOnly(True)
        self.ui.bendCapacity_2.setReadOnly(True)
        self.ui.bendCapacity_3.setReadOnly(True)
        self.ui.bendCapacity_4.setReadOnly(True)
        self.ui.singleLength.setPlaceholderText('150')
        self.ui.singleLength.setPlaceholderText('150')
        self.ui.singleThick.setPlaceholderText('6')
        self.ui.concreteValue.setPlaceholderText('19.10')
        self.ui.steelValue.setPlaceholderText('345')
        self.ui.panelValue.setPlaceholderText('345')
        self.ui.reductFactor.setPlaceholderText('1.0')

        # 单板验证
        self.ui.panelValue_11.setReadOnly(True)
        self.ui.panelValue_12.setReadOnly(True)
        self.ui.panelValue_13.setReadOnly(True)
        self.ui.panelValue_14.setReadOnly(True)
        self.ui.vertifyButton.clicked.connect(self.vertification)
        self.ui.vertifyButton.setToolTip("点击验算结果")

        # 双板连接
        self.ui.pushButton_2.clicked.connect(self.doubleHandle)
        self.ui.pushButton_2.setToolTip("点击计算结果")
        self.ui.axialPressure_3.setReadOnly(True)
        self.ui.bendCapacity_9.setReadOnly(True)
        self.ui.bendCapacity_10.setReadOnly(True)
        self.ui.bendCapacity_11.setReadOnly(True)
        self.ui.bendCapacity_12.setReadOnly(True)
        self.ui.singleLength_2.setPlaceholderText('150')
        self.ui.singleLength_2.setPlaceholderText('150')
        self.ui.singleThick_2.setPlaceholderText('6')
        self.ui.concreteValue_2.setPlaceholderText('19.10')
        self.ui.steelValue_2.setPlaceholderText('345')
        self.ui.panelValue_2.setPlaceholderText('345')
        self.ui.reductFactor_2.setPlaceholderText('1.0')

        # 双板验证
        self.ui.panelValue_17.setReadOnly(True)
        self.ui.panelValue_18.setReadOnly(True)
        self.ui.panelValue_19.setReadOnly(True)
        self.ui.panelValue_20.setReadOnly(True)
        self.ui.vertifyButton_2.clicked.connect(self.vertification_2)
        self.ui.vertifyButton_2.setToolTip("点击验算结果")

    def singleHandle(self):
        self.ui.loading.setText("计算中...")

        # 轴力计算
        l = float(self.ui.singleLength.text()) if self.ui.singleLength.text() else 150
        t = float(self.ui.singleThick.text())   if self.ui.singleThick.text() else 6
        fc = float(self.ui.concreteValue.text()) if self.ui.concreteValue.text() else 19.1
        fy = float(self.ui.steelValue.text())  if self.ui.steelValue.text() else 345
        fyp = float(self.ui.panelValue.text()) if self.ui.panelValue.text() else 345
        faii = float(self.ui.reductFactor.text()) if self.ui.reductFactor.text() else 1
        Aci = (l - 2*t) ** 2
        Asi = l * l - Aci
        Ayp = l * t * 2
        Noi = fy * Asi + Aci * fc
        Nui = faii * Noi
        Nud = (Ayp * fyp + 3 * Nui) / 1000
        self.ui.axialPressure.setText(str(Nud))

        # 弯矩计算
        # 弯矩绕x轴且转向y轴正向
        K1 = (4 * l ** 2 + 4 * t ** 2 - 4 * l * t) * fy
        L1 = (l ** 2 + 4 * t ** 2 - 4 * l * t) * fc
        J1 = 4 * l * fy
        a = (K1 + L1) / J1
        W1 = 2 * l ** 3 + 7 * l ** 2 * t - 6 * l * t ** 2 - 4 * t ** 2 * a + 4 * l * t * a + 2 * l * a ** 2 - 4 * l ** 2 * a
        W2 = 5/2 * l ** 3  - 10 * l ** 2 * t + 10 * l * t ** 2 - l**2*a - 4*t**2*a + 4*l*t*a
        Mux = (W1 * fy + W2 * fc) /10**6
        self.ui.bendCapacity.setText(str(Mux))

        # 弯矩绕x轴且转向y轴正向
        K2 = (4*t**2 - 8*l*t)*fy
        L2 = (4*t**2 - 2*l*t)*fc
        J2 = -8*t*fy - 2*l*fc + 4*t*fc
        a = (K2+L2)/J2
        if a <= l-t and a >= (l+t)/2:
            W1 =  4 * t ** 3 + 15 * l ** 2 * t - 16 * l * t ** 2 + 4 * t ** 2 * a + 4 * t * a**2 - 8 * l * t * a
            W2 =  -2 * t ** 3 + l * t**2 + l*a**2 - 2*t*a**2 + 4*t**2*a - 2*l*t*a
        elif a <= l and a >= l-t:
            K2 = (-4*l*l - 4*t*t + 4*l*t) * fy
            L2 = (2*l*l - 8*l*t + 8*t*t) * fc
            J2 = -4*l*fy
            a = (K2+L2)/J2
            W1 = 2 * l ** 3 + 7 * l ** 2 * t - 6 * l * t ** 2 - 4 * t ** 2 * a + 4 * l * t * a + 2 * l * a ** 2 - 4*l**2*a
            W2 = - l ** 3 + 4*l**2*t -4*l * t**2 + 2*l**2*a + 8*t**2*a -8*t*l*a - 2*l*t*a
        else:
            W1 = 0
            W2 = 0
        Mux2 = (W1 * fy + W2 * fc) /10**6
        self.ui.bendCapacity_2.setText(str(Mux2))

        # 弯矩绕x'轴且转向y'轴正向
        K3 = 28*l*t*fy
        L3 = (7*l**2 - 24*l*t + 10*t**2)*fc
        g2 = math.sqrt(2)
        J3 = 12*g2*t*fy -(6*g2*t - 2*g2*l)*fc
        a = (K3+L3)/J3
        W1 = 6*g2*a*a*t - 28*l*a*t + 89/4*g2*l*l*t
        W2 = (-10/3*a**3 + 16*g2*a*a*l - 8*g2*a*a*t -52*a*l*l + 54*l*a*t - 30*a*t*t + 86/3*g2*l**3
            -137/3*g2*l*l*t + 71/3*g2*t*t*l -7/3*g2*t**3)
        Mux3 = (W1*fy + W2*fc)/10**6
        self.ui.bendCapacity_3.setText(str(Mux3))


        # 弯矩绕x‘轴且转向y’轴负向
        K4 = K3
        L4 = (4*l*t - l*l - 4*t*t)*fc
        J4 = 12*g2*t*fy
        a = (K4 + L4)/J4
        W2 = -g2/2*l**3 + 2*g2*l*l*t + a*l*l - 2*g2*l*t*t - 4*l*a*t + 4*a*t*t
        Mux4 = (W1*fy + W2*fc)/10**6
        self.ui.bendCapacity_4.setText(str(Mux4))

        self.ui.loading.setText("计算完成")

    # 单板验证结果
    def vertification(self):
        self.ui.loading_2.setText("验证中...")
        #判断是否进行了验算
        flag = 0

        Mx = float(self.ui.maxMoment.text()) if self.ui.maxMoment.text() else 0.1
        N = float(self.ui.axialForce.text()) if self.ui.axialForce.text() else 0.1
        betamx = float(self.ui.beiTa.text()) if self.ui.beiTa.text() else 0.1
        gamax = float(self.ui.plasticFactor.text()) if self.ui.plasticFactor.text() else 0.1
        Es = float(self.ui.moduleS.text()) if self.ui.moduleS.text() else 0.1
        Ec = float(self.ui.moduleC.text()) if self.ui.moduleC.text() else 0.1
        slenderityRatio = float(self.ui.slenderRatio.text()) if self.ui.slenderRatio.text() else 1
        gamare = float(self.ui.gamaRE.text()) if self.ui.gamaRE.text() else 1
        Nud = float(self.ui.axialPressure.text()) if self.ui.axialPressure.text() else 1
        Mux1 = float(self.ui.bendCapacity.text()) if self.ui.bendCapacity.text() else 1
        Mux2 = float(self.ui.bendCapacity_2.text()) if self.ui.bendCapacity_2.text() else 1
        Mux3 = float(self.ui.bendCapacity_3.text()) if self.ui.bendCapacity_3.text() else 1
        Mux4 = float(self.ui.bendCapacity_4.text()) if self.ui.bendCapacity_4.text() else 1
        l = float(self.ui.singleLength.text()) if self.ui.singleLength.text() else 150
        t = float(self.ui.singleThick.text()) if self.ui.singleThick.text() else 6
        Ac = (l-2*t)**2*3
        As = l*l*3 - Ac + l*t*2

        #验证Mux1
        if Mux1 == 1:
            self.ui.panelValue_11.setText("请先计算")
            self.ui.loading_2.setText("")
        else:
            flag = 1
            Nex = math.pi*(Es*As + Ec*Ac)/(1.1*slenderityRatio**2)
            force = N / Nud
            moment1 = (betamx*Mx) / (gamax*Mux1*(1-0.8*N/Nex))
            if force + moment1 <= (1/gamare):
                self.ui.panelValue_11.setText("符合要求")
            else:
                self.ui.panelValue_11.setText("不符合")

        # 验证Mux2
        if Mux2 == 1:
            self.ui.panelValue_12.setText("请先计算")
        else:
            moment2 = (betamx * Mx) / (gamax * Mux2 * (1 - 0.8 * N / Nex))
            if force + moment2 <= (1 / gamare):
                self.ui.panelValue_12.setText("符合要求")
            else:
                self.ui.panelValue_12.setText("不符合")

        # 验证Mux3
        if Mux3 == 1:
            self.ui.panelValue_13.setText("请先计算")
        else:
            moment3 = (betamx * Mx) / (gamax * Mux3 * (1 - 0.8 * N / Nex))
            if force + moment3 <= (1 / gamare):
                self.ui.panelValue_13.setText("符合要求")
            else:
                self.ui.panelValue_13.setText("不符合")

        # 验证Mux4
        if Mux4 == 1:
            self.ui.panelValue_14.setText("请先计算")
        else:
            moment3 = (betamx * Mx) / (gamax * Mux4 * (1 - 0.8 * N / Nex))
            if force + moment3 <= (1 / gamare):
                self.ui.panelValue_14.setText("符合要求")
            else:
                self.ui.panelValue_14.setText("不符合")

        if flag:
            self.ui.loading_2.setText("验证完成")


    # 双板连接
    def doubleHandle(self):
        self.ui.loading_3.setText("计算中...")

        # 轴力计算
        l = float(self.ui.singleLength_2.text()) if self.ui.singleLength_2.text() else 150.0
        t = float(self.ui.singleThick_2.text()) if self.ui.singleThick_2.text() else 6.0
        fc = float(self.ui.concreteValue_2.text()) if self.ui.concreteValue_2.text() else 19.1
        fy = float(self.ui.steelValue_2.text()) if self.ui.steelValue_2.text() else 345.0
        fyp = float(self.ui.panelValue_2.text()) if self.ui.panelValue_2.text() else 345.0
        faii = float(self.ui.reductFactor_2.text()) if self.ui.reductFactor_2.text() else 1.0
        Aci = (l - t) ** 2
        Asi = l * l - Aci
        Ayp = l * t * 2
        Noi = fy * Asi + Aci * fc
        Nui = faii * Noi
        Nus = (Ayp * fyp + 3 * Nui) / 1000
        self.ui.axialPressure_3.setText(str(Nus))

        # 弯矩计算
        # 弯矩绕x轴且转向y轴正向


        Kd1 = (5*l*t-2*t*t)*fy
        Ld1 = (3*l*l + 4*t*t - 7*l*t)*fc
        Jd1 = 4*t*fy + 2*l*fc - 2*t*fc
        a = (Kd1 + Ld1)/Jd1
        if a >= l/2 and a <= l-t:
            W1 = -10*l*t*a - 18*l*t*t + 4*t**3 + 18*l*l*t + 4*a*a*t + 4*t*t*a
            W2 = 11/2*l**3 - 5*l*l*a - 18*l*l*t + 13*l*t*a + 31/2*l*t*t - 8*t*t*a + 3/2*l*a*a - 2*t**3 - 2*t*a*a
        else :
            Kd1 = (6*l*l + 4*t*t -4*l*t)*fy
            Ld1 = (2*l*l + 4*t*t -6*l*t)*fc
            Jd1 = 6*l*fy
            a = (Kd1 + Ld1)/Jd1
            if  a>= l-t and a <= l:
                W1 = 4*l*t*a - 7*l*t*t + 3*l**3 + 3*l*a*a - 6*l*l*a + 8*l*l*t - 4*t*t*a
                W2 = 6*l*t*a + 10*l*t*t + 4*l**3 - 2*l*l*a - 13*l*l*t - 4*t*t*a
            else:
                W1 = 0
                W2 = 0
        Mux10 = (W1*fy + W2*fc)/10**6
        self.ui.bendCapacity_10.setText(str(Mux10))


        # 弯矩绕x轴且转向y轴负向

        Kd2 = (10 * l * t - 4 * t * t) * fy
        Ld2 = (3 * l * t - 4 * t * t) * fc
        Jd2 = 8 * t * fy + 3 * l * fc - 4 * t * fc
        a = (Kd2 + Ld2) / Jd2
        if a >= l/2 and a <= l-t:
            W1 = -10*l*t*a - 18*l*t*t + 4*t**3 + 18*l*l*t + 4*a*a*t + 4*t*t*a
            W2 = -3*l*t*a + 3/2*l*t*t - 2*t**3 - 2*t*a*a + 4*a*a*t + 3/2*a*a*l
        else:
            Kd2 = (6*l*l + 4*t*t -4*l*t)*fy
            Ld2 = (10*l*t - 3*l*l - 8*t*t)*fc
            Jd2 = 6*l*fy
            a = (Kd2 + Ld2)/Jd2
            if  a>= l-t and a <= l:
                W1 = 4*l*t*a - 7*l*t*t + 3*l**3 + 3*l*a*a - 6*l*l*a + 8*l*l*t - 4*t*t*a
                W2 = -10*l*t*a - 4*l*t*t - 3/2*l**3 + 3*l*l*a + 5*l*l*t + 8*t*t*a
            else:
                W1 = 0
                W2 = 0

        Mux11 = (W1 * fy + W2 * fc) / 10 ** 6
        self.ui.bendCapacity_11.setText(str(Mux11))

        # 弯矩绕x‘轴且转向y’轴正向

        s2 = math.sqrt(2)
        Kd3 = 14*l*t*fy
        Ld3 = (7/2*l*l + 5/2*t*t -11*l*t)*fc
        Jd3 = 6*s2*t*fy - (3*s2*t - s2*l)*fc
        a = (Kd3 + Ld3)/Jd3
        W1 = -28*l*t*a + 46*s2*l*l*t/2 + 6*s2*a*a*t  #W1计算正确
        W2 = (59*s2/3*l**3 - 3*s2/2*t**3 - 193*s2*l*l*t/6 + 53/3*s2*l*t*t - 2*a**3 - 34*l*l*a
              - 11*a*t*t + 36*l*a*t + 10*s2*l*a*a - 5*s2*t*a*a)
        Mux12 = (W1 * fy + W2 * fc) / 10 ** 6
        self.ui.bendCapacity_12.setText(str(Mux12))

        # 弯矩绕x‘轴且转向y’轴负向
        Kd4 = 14*l*t*fy
        Ld4 = (l*l - 7*t*t/2 - 3*l*t)*fc
        Jd4 = 6*s2*t*fy + (s2*l - 3*s2*t)*fc
        a = (Kd4 + Ld4)/Jd4
        W2 = (-3*s2*l**3 - 4*s2/3*t**3 -5*s2*l*l*t/3 - 28*s2*l*t*t/3 + 5/3*a**3 + 26/3*l*l*a
              + 12*a*t*t + 6*l*a*t - 25*s2/6*l*a*a - 7*s2/2*t*a*a)

        Mux9 = (W1 * fy + W2 * fc) / 10 ** 6
        self.ui.bendCapacity_9.setText(str(Mux9))

        self.ui.loading_3.setText("计算完成")

    # 双板验证结果
    def vertification_2(self):
        self.ui.loading_4.setText("验证中...")
        #判断是否进行了验算
        flag = 0

        Mx = float(self.ui.maxMoment_2.text()) if self.ui.maxMoment_2.text() else 0.1
        N = float(self.ui.axialForce_2.text()) if self.ui.axialForce_2.text() else 0.1
        betamx = float(self.ui.beiTa_2.text()) if self.ui.beiTa_2.text() else 0.1
        gamax = float(self.ui.plasticFactor_2.text()) if self.ui.plasticFactor_2.text() else 0.1
        Es = float(self.ui.moduleS_2.text()) if self.ui.moduleS_2.text() else 0.1
        Ec = float(self.ui.moduleC_2.text()) if self.ui.moduleC_2.text() else 0.1
        slenderityRatio = float(self.ui.slenderRatio_2.text()) if self.ui.slenderRatio_2.text() else 1
        gamare = float(self.ui.gamaRE_2.text()) if self.ui.gamaRE_2.text() else 1
        Nud = float(self.ui.axialPressure_3.text()) if self.ui.axialPressure_3.text() else 1
        Mux1 = float(self.ui.bendCapacity_10.text()) if self.ui.bendCapacity_10.text() else 1
        Mux2 = float(self.ui.bendCapacity_11.text()) if self.ui.bendCapacity_11.text() else 1
        Mux3 = float(self.ui.bendCapacity_12.text()) if self.ui.bendCapacity_12.text() else 1
        Mux4 = float(self.ui.bendCapacity_9.text()) if self.ui.bendCapacity_9.text() else 1
        l = float(self.ui.singleLength_2.text()) if self.ui.singleLength_2.text() else 150
        t = float(self.ui.singleThick_2.text()) if self.ui.singleThick_2.text() else 6
        Ac = (l-2*t)**2*3 + (l-2*t)*l*2
        As = (9 - 4)*l**2 - Ac
        self.ui.panelValue_17.setText("ji")

        #验证Mux1
        if Mux1 == 1:
            self.ui.panelValue_17.setText("请先计算")
            self.ui.loading_4.setText("")
        else:
            flag = 1
            Nex = math.pi*(Es*As + Ec*Ac)/(1.1*slenderityRatio**2)
            force = N / Nud
            moment1 = (betamx*Mx) / (gamax*Mux1*(1-0.8*N/Nex))
            if force + moment1 <= (1/gamare):
                self.ui.panelValue_17.setText("符合要求")
            else:
                self.ui.panelValue_17.setText("不符合")

        # 验证Mux2
        if Mux2 == 1:
            self.ui.panelValue_18.setText("请先计算")
        else:
            moment2 = (betamx * Mx) / (gamax * Mux2 * (1 - 0.8 * N / Nex))
            if force + moment2 <= (1 / gamare):
                self.ui.panelValue_18.setText("符合要求")
            else:
                self.ui.panelValue_18.setText("不符合")

        # 验证Mux3
        if Mux3 == 1:
            self.ui.panelValue_19.setText("请先计算")
        else:
            moment3 = (betamx * Mx) / (gamax * Mux3 * (1 - 0.8 * N / Nex))
            if force + moment3 <= (1 / gamare):
                self.ui.panelValue_19.setText("符合要求")
            else:
                self.ui.panelValue_19.setText("不符合")

        # 验证Mux4
        if Mux4 == 1:
            self.ui.panelValue_20.setText("请先计算")
        else:
            moment3 = (betamx * Mx) / (gamax * Mux4 * (1 - 0.8 * N / Nex))
            if force + moment3 <= (1 / gamare):
                self.ui.panelValue_20.setText("符合要求")
            else:
                self.ui.panelValue_20.setText("不符合")

        if flag:
            self.ui.loading_4.setText("验证完成")

if __name__ == '__main__':
    app = QApplication([])
    stats = Stats()
    stats.ui.show()
    app.exec_()
